package team.seekers.hula.question.questionBonfire.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import team.seekers.hula.question.questionBonfire.entity.dao.BonfireDO;
import team.seekers.hula.question.questionBonfire.entity.dto.BonfireSaveDTO;
import team.seekers.hula.question.questionBonfire.entity.dto.BonfireUpdateDTO;
import team.seekers.hula.question.questionBonfire.entity.vo.BonfireInfoVO;
import team.seekers.hula.question.questionBonfire.entity.vo.BonfireListVO;
import team.seekers.hula.question.questionBonfire.mapper.BonfireMapper;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.user.user.service.UserService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hbc
 * @since 2022-11-22
 */
@Service
public class BonfireService extends ServiceImpl<BonfireMapper, BonfireDO> {

    @Resource
    private UserService userService;


    public String save(BonfireSaveDTO saveDTO) {
        BonfireDO bonfireDO = new BonfireDO();
        BeanUtils.copyProperties(saveDTO,bonfireDO);
        save(bonfireDO);
        return bonfireDO.getId();
    }

    public void remove(String id) {
        removeById(id);
    }

    public BonfireInfoVO info(String id) {
        BonfireDO bonfireDO = getById(id);
        BonfireInfoVO bonfireInfoVO = new BonfireInfoVO();
        BeanUtils.copyProperties(bonfireDO,bonfireInfoVO);
        bonfireInfoVO.setUserInfo(userService.getFollowInfoVO(bonfireDO.getUserId(), MindUtils.getUserId()));
        return bonfireInfoVO;
    }

    public void update(BonfireUpdateDTO updateVO) {
        BonfireDO bonfireDO = new BonfireDO();
        BeanUtils.copyProperties(updateVO,bonfireDO);
        updateById(bonfireDO);
    }

    public List<BonfireListVO> listVO(){
        List<BonfireDO> list = list();
        ArrayList<BonfireListVO> bonfireListVOS = new ArrayList<>();

        for (BonfireDO bonfireDO : list) {
            BonfireListVO bonfireListVO = new BonfireListVO();
            BeanUtils.copyProperties(bonfireDO,bonfireListVO);
            if(MindUtils.isLogin()){
                bonfireListVO.setUserInfo(userService.getFollowInfoVO(bonfireDO.getUserId(), MindUtils.getUserId()));
            }else {
                bonfireListVO.setUserInfo(userService.getFollowInfoVO(bonfireDO.getUserId(), null));
            }

            bonfireListVOS.add(bonfireListVO);
        }

        return bonfireListVOS;
    }
}
